package com.lzy.cloud.base.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.ShopBankDto;
import com.lzy.cloud.base.facade.ShopBankFacade;
import com.lzy.cloud.base.request.ShopBankRequest;
import com.lzy.cloud.base.response.ShopBankResponse;
import com.lzy.cloud.base.response.UserToken;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商家开户银行
 *
 * @author lc
 * @version 2018/8/16
 */
@RestController
@RequestMapping("/shop/bank")
public class ShopBankController {

    @Autowired
    private ShopBankFacade shopBankFacade;

    @Autowired
    private BaseController baseController;

    /**
     * 添加账户
     * @param request
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Result create(@RequestHeader("token")String token,@RequestBody ShopBankRequest request) {
        Ensure.that(request).isNotNull("00000002");
        Result userLogin = baseController.isUserLogin(token);
        UserToken userToken = DozerUtils.map(userLogin.getResult(), UserToken.class);
        request.setShopId(userToken.getPlatformId());
        Long id = shopBankFacade.create(DozerUtils.map(request, ShopBankDto.class));
        Ensure.that(id).isGt(0, "00030002");
        return new Result(id);
    }

    /**
     * 根据卡号查询
     * @param account
     * @return
     */
    @RequestMapping(value = "/find/account/{account}",method = RequestMethod.GET)
    public Result findByAccount(@PathVariable("account") String account) {
        Ensure.that(account).isNotNull("参数为空");
        ShopBankDto shopBankDto = shopBankFacade.findByAccount(account);
        Ensure.that(shopBankDto).isNotNull("查询失败");
        return new Result(DozerUtils.map(shopBankDto,ShopBankResponse.class));
    }

    /**
     * 分页查询
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result selectList(ShopBankRequest request) {
        Ensure.that(request).isNotNull("参数为空");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<ShopBankDto> pageInfo = shopBankFacade.selectList(pager, DozerUtils.map(request, ShopBankDto.class));
        pager.setTotal(Long.valueOf(pageInfo.getList().size()));
        return new Result(pager,DozerUtils.maps(pageInfo.getList(),ShopBankResponse.class));
    }

    /**
     * 编辑@RequestHeader("token")String token,
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody ShopBankRequest request) {
        Ensure.that(request).isNotNull("00000002");
//        Result userLogin = baseController.isUserLogin(token);
//        UserToken userToken = DozerUtils.map(userLogin.getResult(), UserToken.class);
//        request.setShopId(userToken.getPlatformId());
        Boolean ok = shopBankFacade.update(DozerUtils.map(request, ShopBankDto.class));
        return new Result(ok);
    }
}
